# -*- coding: iso-8859-1 -*-


"""
Hexagone class - contains everything usefull to manage hexs

Catanyste - Copyright 2008-2009 Julien RAIGNEAU
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance 
with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
the specific language governing permissions and limitations under the License. 

@author J.Raigneau
"""

import logging
import math
from hexagone import Hexagone,ListHexagone, Thief
from hexmanager import HexManager
from utils import opj
from gamecomponents import GameComponents, ResourceFieldManager
from hexacoordinate import HexaCoordinate
from vertex import Vertex

class Board(object):
    
    thief = None
    listeHexagoneTriee = None  #dict --> random access --> not good for algo --> need list
    hexmanager = None
    hexacoord = None
    resource_manager = None
    hexL = 0
    hexl = 0

    def __init__(self):
        """
        Inits Board class
        """
        #TODO: Manage board size + options (number of player)
        self.thief = Thief((99,99))
        #self.listeHexagoneTriee = ListHexagone() #hack
        self.listeHexagoneTriee = HexManager((10,10))

        self.defCoord()     #coordinate system
        self.defHexes()     # hexes with resource
        self.defValue()     # value on hexes

    def defCoord(self):
        """
        mount a coordinate system
        """
        self.orient = HexaCoordinate.UPDOWN
        L = self.hexL = 80
        l = self.hexl = math.sqrt(3) * self.hexL / 2.0    #perfect Hex
        self.hexacoord = HexaCoordinate(self.orient, (L,l))

    def defHexes(self, nb_of_player=4):
        """
        define board's hexes
        """
        self.resource_manager = manager = ResourceFieldManager()
        #hack manager while not using port3 and port2
        manager.resource_fields[GameComponents.SEA_FIELDS][GameComponents.WATER] = 18
        manager.resource_fields_ref[GameComponents.SEA_FIELDS][GameComponents.WATER] = 18
        #--end hack
        # constants to improve code readibiliy
        takeRand = getattr(manager,'takeRandomEarthField')
        takeWater_ = getattr(manager,'takeResourceField')(GameComponents.WATER)

        for u in range(-4, 4):
            for v in range (-4, 4):
                if self.hexacoord.distanceD6((u,v)) < 3:
                    hexa = Hexagone(self, (u,v), takeRand(),99)
                    hexa.hexacoord = self.hexacoord   #attach the same HexaCoordinate to the hexes
                    if hexa.matiere == Hexagone.DESERT:
                        self.PutThief(hexa)
                    self.listeHexagoneTriee.AddHexagone(hexa)
                   # listeHexa.append(((u,v), takeRand()))
                elif self.hexacoord.distanceD6((u,v)) == 3:
                   # listeHexa.append(((u,v), takeWater_))
                    hexa = Hexagone(self, (u,v), takeWater_,99)
                    hexa.hexacoord = self.hexacoord   #attach the same HexaCoordinate to the hexes
                    self.listeHexagoneTriee.AddHexagone(hexa)
                else :
                    pass

        #we also need to define vertexes
        #ugly for now
        for hex in self.listeHexagoneTriee.hexes:
            if hex:
                u0, v0 = hex.uv
                vertex1 = Vertex((u0+1./3.,v0-1./3.))
                vertex1.hexacoord = self.hexacoord
                vertex2 = Vertex((u0-1./3.,v0+1./3.))
                vertex2.hexacoord = self.hexacoord
                self.listeHexagoneTriee.setVertex(vertex1)
                self.listeHexagoneTriee.setVertex(vertex2)

        self.hexmanager = self.listeHexagoneTriee     #new name ?

    def defValue(self):
        """
        Put the value on the hexes
        """
        hexmanager = self.listeHexagoneTriee
        #I don't want to think for now
        for_chip = ((2,0),(2,1),(2,2),(1,2),(0,2),(-1,1),(-2,0),(-2,-1),(-2,-2),(-1,-2),(0,-2),(1,-1),
                    (1,0),(1,1),(0,1),(-1,0),(-1,-1),(0,-1),
                    (0,0))
        i = 0
        for uv in for_chip:
            hex = hexmanager.GetHexagone(uv)
            if hex:
                if hex.matiere != Hexagone.DESERT:
                    hex.valeur = GameComponents.chips[i]
                    i += 1
        #finally, compute value at vertex
        hexmanager.computeVertexValues()

    def PutThief(self, hex):
        """
        Puts thief on hex
        """
        self.thief.SetThief(hex)
        if self.hexmanager: self.hexmanager.computeVertexValues() #recompute values
         
    def GetThief(self):
        """
        Returns thief coord
        """
        return self.thief.GetCoord()
                